import * as echarts from '../../ec-canvas/echarts';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifTeacher: false,
    courseId:"",
    studentId:"",
    classId:"",
    coursetStuInfo:{},
    canshu:{},

    //平均心率
     avg_heart_min:0,
     avg_heart_max:0,
     heart_high:0,
    //运动密度
     exercise_density_min:0,
     exercise_density_max:0,
     
    //运动强度
     exercise_density_min:0,
     exercise_power_min:0,
     power_exponent:0, //极限警示
     effective_movement_exponent:0,

     heart_base_min:0,
     heart_base_max:0,

     heart_exponent_min:0,
     heart_exponent_max:0,

    //饼图数据
    lineEc:{
      lazyLoad: true,
    },
    timeValList:[],
    boyLine:[],
    girlLine:[],
    classLine:[],
  },



//折线图
drawLine:function(){
  let ecComponentdrawBar = this.selectComponent('#mychart-dom-line');
  ecComponentdrawBar.init((canvas, width, height, dpr) => {
    // 获取组件的 canvas、width、height 后的回调函数
    // 在这里初始化图表
    const chart = echarts.init(canvas, null, {
      width: width,
      height: height,
      devicePixelRatio: dpr // new
    });
  
    canvas.setChart(chart);

    var option = {
      title: {
        text: '平均心率曲线',
        textStyle:{
          fontWeight: 'bolder',
          fontSize:'13.5',
          color: '#000',
  
        }
        
      },
      legend: {
        x:'right',
        y:'top',
        textStyle:{
          //fontWeight:bold,
          fontSize:'12',
          color: '#a9a8ad',
        },
        //data: ['男生平均', '女生平均', '全班平均']
        data: ['个人心率', '性别心率', '全班平均']
        
      },
      tooltip: {
        trigger: 'axis'
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      // toolbox: {
      //   feature: {
      //     saveAsImage: {}
      //   }
      // },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: this.data.timeValList,
        axisLabel: {
          fontSize:11,
          color: '#95969d', 
          rotate:'35',// 旋转度数
        },
        axisLine: {//y轴线的配置
          show: true,//是否展示
          lineStyle: {
              color: "#95969d",//y轴线的颜色（若只设置了y轴线的颜色，未设置y轴文字的颜色，则y轴文字会默认跟设置的y轴线颜色一致）
              width: 1,//y轴线的宽度
              type: "solid"//y轴线为实线
            },
          }
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '个人心率',
          type: 'line',
         
          data: this.data.boyLine
        },
        {
          name: '全班平均',
          type: 'line',
         
          data: this.data.classLine
        },
        {
          name: '性别心率',
          type: 'line',
         
          data:this.data.girlLine
        }
      ]
      
    };
    chart.setOption(option);
    return chart;
  });
},


setLineData(data){
  var boyLine =[];
  var girlLine =[];
  var classLine =[];
  var timeValList =[];
  if(data.personalLine!=null){
    boyLine = data.personalLine.split(",")
  }
  if(data.genderLine!=null){
    girlLine = data.genderLine.split(",")
  }
  if(data.classLine!=null){
    classLine = data.classLine.split(",")
  }
  var endTime = parseInt(data.endTime.substring(0,2))*60+parseInt(data.endTime.substring(3,5));
  var startTime = parseInt(data.startTime.substring(0,2))*60+parseInt(data.startTime.substring(3,5));
  var time = (endTime-startTime)*60;
  //console.log("-----1--------"+time);
  for(var i=0;(i*5)<time;i++){
    var val = (i*5);
        var fen = parseInt(val/60);
        var miao = val%60;
        if(fen > 0){
          val = fen+"'"+miao+"''"
        }else{
          val = miao+"''"
        }
        //console.log(val+"="+fen+"="+miao);
      timeValList.push(val)
  }
  console.log("-----1--------"+boyLine.join(','));
  console.log("-----2--------"+girlLine.join(','));
  console.log("-----3--------"+classLine.join(','));
  this.setData({
    
    boyLine:boyLine,
    girlLine:girlLine,
    classLine:classLine,
    timeValList:timeValList
  })

 this.drawLine()
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('接口数据当前用户角色:'+wx.getStorageSync('ifTeacher'))
    this.setData({
      courseId:options.courseId,
      studentId:options.studentId,
      classId:options.classId,
      ifTeacher:wx.getStorageSync('ifTeacher')
    })

    this.getstandard()
  },
//课堂记录-班级参数标准
  getstandard(){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    var data = {
      classId:this.data.classId,
      classType:"0"
    }
    console.log('班级参数标准接口数据='+JSON.stringify(data))
    app.api.standard(data).then(rest => {
      wx.hideLoading()
    
      if(rest&&rest.success){       
        if(rest.data!=null && rest.data.list.length>0){
          console.log('接口数据4q='+JSON.stringify(rest.data))
          //平均心率
          var avg_heart_min;
          var avg_heart_max;
          var heart_high;
          //运动密度
          var exercise_density_min;
          var exercise_density_max;
           
          //运动强度
          var exercise_density_min;

          var heart_base_min;
          var heart_base_max;

          var heart_exponent_min;
          var heart_exponent_max;

          var exercise_power_min;
          //var power_exponent;
          var effective_movement_exponent;

          for(var i=0;i<rest.data.list.length;i++){
            if("avg_heart_min"==rest.data.list[i].keyInfo){
              avg_heart_min = rest.data.list[i].valueInfo
            }else if("avg_heart_max"==rest.data.list[i].keyInfo){
              avg_heart_max = rest.data.list[i].valueInfo
            }else if("heart_high"==rest.data.list[i].keyInfo){
              heart_high = rest.data.list[i].valueInfo
            }

            else if("exercise_density_min"==rest.data.list[i].keyInfo){
              exercise_density_min = rest.data.list[i].valueInfo
            }else if("exercise_density_max"==rest.data.list[i].keyInfo){
              exercise_density_max = rest.data.list[i].valueInfo
            }

            else if("exercise_density_min"==rest.data.list[i].keyInfo){
              exercise_density_min = rest.data.list[i].valueInfo
            }else if("exercise_power_min"==rest.data.list[i].keyInfo){
              exercise_power_min = rest.data.list[i].valueInfo
            }

            // else if("power_exponent"==rest.data.list[i].keyInfo){
            //   power_exponent = rest.data.list[i].valueInfo
            // }
            else if("effective_movement_exponent"==rest.data.list[i].keyInfo){
              effective_movement_exponent = rest.data.list[i].valueInfo
            }

            else if("heart_base_min"==rest.data.list[i].keyInfo){
              heart_base_min = rest.data.list[i].valueInfo
            }else if("heart_base_max"==rest.data.list[i].keyInfo){
              heart_base_max = rest.data.list[i].valueInfo
            }

            else if("heart_exponent_min"==rest.data.list[i].keyInfo){
              heart_exponent_min = rest.data.list[i].valueInfo
            }else if("heart_exponent_max"==rest.data.list[i].keyInfo){
              heart_exponent_max = rest.data.list[i].valueInfo
            }

           
          }

         
          this.setData({
            canshu:rest.list,
            //平均心率
            avg_heart_min:avg_heart_min,
            avg_heart_max:avg_heart_max,
            heart_high:heart_high,
            //运动密度
            exercise_density_min:exercise_density_min,
            exercise_density_max:exercise_density_max,
            
            //运动强度
            exercise_density_min:exercise_density_min,
            exercise_power_min:exercise_power_min,
            // power_exponent:power_exponent,
             effective_movement_exponent:effective_movement_exponent,

            heart_base_min:heart_base_min,
            heart_base_max:heart_base_max,

            heart_exponent_min:heart_exponent_min,
            heart_exponent_max:heart_exponent_max,

             
          })

        }


       
      }else{
        // console.log('接口数据5q=='+rest.errMsg)
        // wx.showToast({
        //   title: rest.errMsg||'网络异常，请稍后重试！！',
        //   icon: 'none',
        //   duration: 2000,
        //   mask:true
        // })
      }
      if(this.data.ifTeacher){
        this.getTeacherStudentInfo()
      }else{
        this.getStudentInfo()
      }
    })
    .catch(r => {
      wx.hideLoading()
      if(this.ifTeacher){
        this.getTeacherStudentInfo()
      }else{
        this.getStudentInfo()
      }
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


  getTeacherStudentInfo(){

    var data = {
      courseId:this.data.courseId,
      studentId:this.data.studentId
    }
    console.log('Teacher接口数据='+JSON.stringify(data))
    app.api.getTeacherStudentInfo(data).then(rest => {
     
      if(rest&&rest.success){  
        console.log('接口数据6q='+JSON.stringify(rest))
        var power_exponent = (220-rest.data.age)*0.9
        this.setData({
          power_exponent:parseInt(power_exponent),
          coursetStuInfo:rest.data,
        })
        this.setLineData(rest.data)
        
      }else{
        console.log('接口数据1q=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


  getStudentInfo(){

    var data = {
      courseId:this.data.courseId,
      studentId:this.data.studentId
    }
    console.log('tStu接口数据='+JSON.stringify(data))
    app.api.getCoursetStuInfo(data).then(rest => {
     
      if(rest&&rest.success){   
        console.log('接口数据22q='+JSON.stringify(rest))
        var power_exponent = (220-rest.data.age)*0.9
        this.setData({
          power_exponent:parseInt(power_exponent),
          coursetStuInfo:rest.data,
        })
        this.setLineData(rest.data)      
      }else{
        console.log('接口数据3q=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})